﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.ComponentModel;

namespace BbsbWebService.Classes
{
    [DataContract]
    public class BaseItem : INotifyPropertyChanged
    {
        [DataMember]
        public Guid Id { get; set; }

        private string _name;

        [DataMember]
        public string Name
        {
            get { return _name; }
            set
            {
                if (_name != value)
                {
                    _name = value;
                    NotifyPropertyChanged("Name");
                }
            }
        }

        [DataMember(EmitDefaultValue=false)]
        public bool IsImportant { get; set; }

        public ValidationState ValidationState
        {
            get
            {
                var state = new ValidationState();
                Validate(ref state);
                return state;
            }
        }

        protected virtual void Validate(ref ValidationState state)
        {
            if (Name.IsNullOrEmpty())
                state.AddError("Must have a name");
        }

        protected void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
